/**
 * 路由配置
 * 定义所有API路由
 */
const Router = require('koa-router');
const chatController = require('../controllers/chat');
const sseController = require('../controllers/sse');

const router = new Router();

// API前缀
const API_PREFIX = '/api';

/**
 * 根路由
 */
router.get('/', async (ctx) => {
  ctx.body = {
    success: true,
    message: 'Travel Agent API Server',
    version: '1.0.0',
    timestamp: new Date().toISOString()
  };
});

/**
 * 健康检查路由
 */
router.get('/api/health', async (ctx) => {
  ctx.body = {
    success: true,
    data: {
      status: 'healthy',
      timestamp: new Date().toISOString(),
      version: '1.0.0'
    }
  };
});

/**
 * SSE连接路由
 */
router.get(`${API_PREFIX}/sse/connect`, sseController.connect.bind(sseController));

/**
 * 聊天相关路由
 */
// 创建新对话
router.post(`${API_PREFIX}/conversations`, chatController.createConversation.bind(chatController));

// 获取对话信息
router.get(`${API_PREFIX}/conversations/:conversationId`, chatController.getConversation.bind(chatController));

// 清理对话
router.delete(`${API_PREFIX}/conversations/:conversationId`, chatController.clearConversation.bind(chatController));

// 发送消息
router.post(`${API_PREFIX}/chat/message`, chatController.sendMessage.bind(chatController));

// 获取可用工具
router.get(`${API_PREFIX}/tools`, chatController.getAvailableTools.bind(chatController));

/**
 * 工具状态路由
 */
router.get(`${API_PREFIX}/tools/status`, async (ctx) => {
  try {
    // 检查MCP工具状态
    const mcpStatus = {
      enabled: false,
      tools: [],
      lastUpdate: new Date().toISOString()
    };

    // 检查LLM服务状态
    const llmStatus = {
      available: true,
      model: 'qwen-plus',
      lastCheck: new Date().toISOString()
    };

    ctx.body = {
      success: true,
      data: {
        mcp: mcpStatus,
        llm: llmStatus,
        server: {
          uptime: process.uptime(),
          memory: process.memoryUsage(),
          version: '1.0.0'
        }
      }
    };
  } catch (error) {
    ctx.status = 500;
    ctx.body = {
      success: false,
      error: {
        message: '获取工具状态失败',
        code: 'TOOLS_STATUS_ERROR'
      }
    };
  }
});

module.exports = router;